package com.bz.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.bz.common.Codeenum;
import com.bz.common.ResponseDto;
import com.bz.dao.MaterialCompleteInDao;
import com.bz.dao.MaterialCompleteInTrackCodeDao;
import com.bz.dao.MaterialCompleteOutDao;
import com.bz.dao.MaterialCompleteOutTrackCodeDao;
import com.bz.dao.PackageInAndOutInDao;
import com.bz.dao.PackageMoveInDao;
import com.bz.dao.RemoteDeviceCtrlOutDao;
import com.bz.dao.RemoteTaskCtrlOutDao;
import com.bz.entity.MaterialCompleteIn;
import com.bz.entity.MaterialCompleteInTrackCode;
import com.bz.entity.MaterialCompleteOut;
import com.bz.entity.MaterialCompleteOutTrackCode;
import com.bz.entity.PackageInAndOutIn;
import com.bz.entity.PackageMoveIn;
import com.bz.entity.RemoteDeviceCtrlOut;
import com.bz.entity.RemoteTaskCtrlOut;
// import com.bz.service.IAimodelService;
import com.bz.model.WorkTaskDelete;

@RestController
@RequestMapping(value = "/api/hmi")
public class HmiController {

    @Autowired
    private MaterialCompleteInDao           materialCompleteInDao;
    @Autowired
    private MaterialCompleteInTrackCodeDao  materialCompleteInTrackCodeDao;
    @Autowired
    private MaterialCompleteOutDao          materialCompleteOutDao;
    @Autowired
    private MaterialCompleteOutTrackCodeDao materialCompleteOutTrackCodeDao;
    @Autowired
    private PackageInAndOutInDao            packageInAndOutInDao;
    @Autowired
    private PackageMoveInDao                packageMoveInDao;
    @Autowired
    private RemoteDeviceCtrlOutDao          remoteDeviceCtrlOutDao;
    @Autowired
    private RemoteTaskCtrlOutDao            remoteTaskCtrlOutDao;

    /**
     * 2.2.2. 工单工艺数据删除（提供）
     * 
     * @param deviceId
     * @param orderSendBeginTime
     * @param orderSendEndTime
     * @return
     */
    @PostMapping(value = "/work-task-delete")
    public ResponseDto<String> workTaskDelete(@RequestBody WorkTaskDelete data) {
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.7. 远程控制接口1（提供，自动模式生效，只做调试测试）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/remote-device-ctrl-out")
    public ResponseDto<String> remoteDeviceCtrlOut(@RequestBody RemoteDeviceCtrlOut data) {
        remoteDeviceCtrlOutDao.insert(data);
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.8. 远程控制接口2（任务相关，调整加工队列，自动模式生效）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/remote-task-ctrl-out")
    public ResponseDto<String> remoteTaskCtrlOut(@RequestBody RemoteTaskCtrlOut data) {
        remoteTaskCtrlOutDao.insert(data);
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.13. 工装出入库（提供）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/package-inAndOut-in")
    public ResponseDto<String> packageInAndOutIn(@RequestBody PackageInAndOutIn data) {
        packageInAndOutInDao.insert(data);
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.14. 上下料（提供）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/package-move-in")
    public ResponseDto<String> packageMoveIn(@RequestBody PackageMoveIn data) {
        packageMoveInDao.insert(data);
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.15. 来料已达到（提供）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/material-complete-in")
    public ResponseDto<String> materialCompleteIn(@RequestBody List<MaterialCompleteIn> datas) {
        if (CollectionUtils.isEmpty(datas)) {
            return new ResponseDto<>(Codeenum.PARAM_ERROR);
        }
        for (MaterialCompleteIn data : datas) {
            materialCompleteInDao.insert(data);
            List<String> ts = data.getTrackCode();
            if (!CollectionUtils.isEmpty(ts)) {
                for (String t : ts) {
                    MaterialCompleteInTrackCode tc = new MaterialCompleteInTrackCode();
                    tc.setMaterialCompleteInId(data.getId());
                    tc.setTrackCode(t);
                    materialCompleteInTrackCodeDao.insert(tc);
                }
            }
        }
        return new ResponseDto<>(Codeenum.SUCCESS);
    }

    /**
     * 2.2.16. 卸料已完成（提供）
     * 
     * @param json
     * @return
     */
    @PostMapping(value = "/material-complete-out")
    public ResponseDto<String> materialCompleteOut(@RequestBody List<MaterialCompleteOut> datas) {
        if (CollectionUtils.isEmpty(datas)) {
            return new ResponseDto<>(Codeenum.PARAM_ERROR);
        }
        for (MaterialCompleteOut data : datas) {
            materialCompleteOutDao.insert(data);
            List<String> ts = data.getTrackCode();
            if (!CollectionUtils.isEmpty(ts)) {
                for (String t : ts) {
                    MaterialCompleteOutTrackCode tc = new MaterialCompleteOutTrackCode();
                    tc.setMaterialCompleteOutId(data.getId());
                    tc.setTrackCode(t);
                    materialCompleteOutTrackCodeDao.insert(tc);
                }
            }
        }
        return new ResponseDto<>(Codeenum.SUCCESS);
    }
}
